﻿using System.Threading.Tasks;
using JetBrains.Annotations;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;

namespace CommonManagement
{
    /// <summary>
    /// 用户管理类
    /// </summary>
    public class UserManager : DomainService
    {
        private readonly IRepository<User, int> _userRepository;

        public UserManager(
            IRepository<User, int> userRepository)
        {
            _userRepository = userRepository;
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="avatar"></param>
        /// <param name="nickName"></param>
        /// <param name="sex"></param>
        /// <param name="phonenumber"></param>
        /// <param name="email"></param>
        /// <param name="idCard"></param>
        public async Task<User> CreateAsync(
            [NotNull] string userName,
            [NotNull] string password,
            string avatar,
            string nickName,
            [NotNull] string sex,
            [NotNull] string phonenumber,
            string email,
            string idCard)
        {
            if (await _userRepository.AnyAsync(u => u.UserName == userName))
                throw new UserNameAlreadyExistsException(userName);
            return await _userRepository.InsertAsync(
                new User(
                    userName,
                    password,
                    avatar,
                    nickName,
                    sex,
                    phonenumber,
                    email,
                    idCard
                )
            );
        }
    }
}